package someclient

import (
	"gitee.com/firewing_group/configenv"
	"gitee.com/firewing_group/model/ctxdata"
	"gitee.com/firewing_group/model/model/fakeredis"

	mgo "gopkg.in/mgo.v2"
)

var db *mgo.Database

//////////////// ex client
var CommonAPILockClient *fakeredis.LockWithExClient

//////////////// number client
var AideLookClient *fakeredis.NumberClient

func init() {
	mongohost := configenv.GetEnvOrConfig("MONGOIP")
	dbname := configenv.GetEnvOrConfig("SELF_DBNAME")
	session, err := mgo.Dial(mongohost)
	if err != nil {
		panic("fake redis init")
	}
	db = session.DB(dbname)
	cc := ctxdata.New(nil, session.DB(dbname), nil)
	///// ex client
	CommonAPILockClient = fakeredis.NewLockWithExClient(cc, configenv.GetEnvOrConfig("CommonAPILockClient"), 3) // 用于 api lock，防止刷
	///// number client
	AideLookClient = fakeredis.NewNumberClient(cc, configenv.GetEnvOrConfig("AideLookClient"))
	{
		// special client : config client
		configCC := ctxdata.New(nil, session.DB(configenv.GetEnvOrConfig("ConfigMongoDB")), nil)
		getterclient := fakeredis.NewStringClient(configCC, configenv.GetEnvOrConfig("ConfigMongoCollection"))
		setterclient := fakeredis.NewStringClient(configCC, configenv.GetEnvOrConfig("ConfigMongoCollection")+"_activate")
		configenv.SetDefaultEtter(getterclient, setterclient)
	}
}
